package com.sikaryofficial.workbench.controller;

import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.StringUtils;
import com.sikaryofficial.common.core.utils.file.FileTypeUtils;
import com.sikaryofficial.common.core.utils.file.MimeTypeUtils;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.system.api.RemoteFileService;
import com.sikaryofficial.system.api.domain.SysFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

/**
 * 工作台公共服务
 *
 * @author qinjinyuan
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/public")
@Api(tags = "工作台公共服务")
@Slf4j
@RefreshScope
public class WorkBenchPublicController extends BaseController {
    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 渠道商图片上传
     */
    @PostMapping("/register/upload")
    @ApiOperation("渠道商注册上传")
    public R<String> upload1(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            String extension = FileTypeUtils.getExtension(file);
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.REGISTER_ALLOWED_EXTENSION)) {
                return R.fail("The file format is incorrect, please upload" + Arrays.toString(MimeTypeUtils.REGISTER_ALLOWED_EXTENSION) + " file format");
            }
            return uploadFile(file);
        }
        return R.fail("Upload image exception, please contact the administrator");
    }
    /**
     * 渠道商图片上传
     */
    @PostMapping("/upload")
    @ApiOperation("渠道商图片上传")
    public R<String> upload(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            String extension = FileTypeUtils.getExtension(file);
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
                return R.fail("The file format is incorrect, please upload" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + " file format");
            }
            return uploadFile(file);
        }
        return R.fail("Upload image exception, please contact the administrator");
    }

    private R<String> uploadFile(MultipartFile file) {
        R<SysFile> fileResult = remoteFileService.upload(file);
        if (StringUtils.isNull(fileResult)) {
            return R.fail("File service exception, please contact the administrator");
        }
        if (StringUtils.isNull(fileResult.getData())) {
            return R.fail("File service exception, please contact the administrator" + fileResult.getMsg());
        }
        String url = fileResult.getData().getUrl();
        return R.ok(url, "success.");
    }
}
